"""
游戏结束对话框
显示游戏结果
"""

import pygame
import sys


class GameOverDialog:
    """游戏结束对话框类"""
    
    def __init__(self, result_text, width=400, height=250):
        """
        初始化对话框
        :param result_text: 结果文本（如"你赢了！"、"AI获胜！"等）
        :param width: 窗口宽度
        :param height: 窗口高度
        """
        self.width = width
        self.height = height
        self.result_text = result_text
        
        # 初始化Pygame（如果还没初始化）
        if not pygame.get_init():
            pygame.init()
        
        self.screen = pygame.display.set_mode((self.width, self.height))
        pygame.display.set_caption("游戏结束")
        self.clock = pygame.time.Clock()
        
        # 颜色定义
        self.COLOR_BG = (250, 250, 250)
        self.COLOR_OVERLAY = (0, 0, 0, 128)  # 半透明黑色
        self.COLOR_TITLE = (50, 50, 50)
        self.COLOR_BUTTON = (100, 150, 200)
        self.COLOR_BUTTON_HOVER = (120, 170, 220)
        self.COLOR_BUTTON_TEXT = (255, 255, 255)
        self.COLOR_BORDER = (80, 80, 80)
        
        # 字体
        try:
            self.title_font = pygame.font.SysFont('microsoftyahei', 36, bold=True)
            self.button_font = pygame.font.SysFont('microsoftyahei', 24)
        except:
            try:
                self.title_font = pygame.font.SysFont('simhei', 36, bold=True)
                self.button_font = pygame.font.SysFont('simhei', 24)
            except:
                self.title_font = pygame.font.SysFont('arial', 36, bold=True)
                self.button_font = pygame.font.SysFont('arial', 24)
        
        # 创建按钮
        self.buttons = self.create_buttons()
        self.selected_action = None
        
    def create_buttons(self):
        """创建按钮"""
        button_width = 150
        button_height = 50
        spacing = 20
        start_x = (self.width - button_width * 2 - spacing) // 2
        button_y = 150
        
        buttons = {
            'restart': {
                'rect': pygame.Rect(start_x, button_y, button_width, button_height),
                'text': '再来一局',
                'action': 'restart'
            },
            'menu': {
                'rect': pygame.Rect(start_x + button_width + spacing, button_y, button_width, button_height),
                'text': '返回菜单',
                'action': 'menu'
            }
        }
        return buttons
    
    def draw_dialog(self):
        """绘制对话框"""
        # 填充背景
        self.screen.fill(self.COLOR_BG)
        
        # 绘制结果文本
        title_surface = self.title_font.render(self.result_text, True, self.COLOR_TITLE)
        title_rect = title_surface.get_rect(center=(self.width // 2, 70))
        self.screen.blit(title_surface, title_rect)
        
        # 绘制按钮
        mouse_pos = pygame.mouse.get_pos()
        
        for name, button_info in self.buttons.items():
            rect = button_info['rect']
            
            # 检查鼠标悬停
            is_hover = rect.collidepoint(mouse_pos)
            color = self.COLOR_BUTTON_HOVER if is_hover else self.COLOR_BUTTON
            
            # 绘制按钮背景
            pygame.draw.rect(self.screen, color, rect, border_radius=8)
            pygame.draw.rect(self.screen, self.COLOR_BORDER, rect, 2, border_radius=8)
            
            # 绘制按钮文字
            text_surface = self.button_font.render(button_info['text'], True, self.COLOR_BUTTON_TEXT)
            text_rect = text_surface.get_rect(center=rect.center)
            self.screen.blit(text_surface, text_rect)
    
    def handle_click(self, pos):
        """
        处理鼠标点击事件
        :param pos: 鼠标位置
        :return: 选择的动作 ('restart', 'menu') 或 None
        """
        for name, button_info in self.buttons.items():
            if button_info['rect'].collidepoint(pos):
                return button_info['action']
        return None
    
    def run(self):
        """运行对话框主循环"""
        running = True
        
        while running:
            # 处理事件
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return 'menu'  # 关闭窗口返回菜单
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:  # 左键点击
                        action = self.handle_click(event.pos)
                        if action:
                            self.selected_action = action
                            return action
            
            # 绘制界面
            self.draw_dialog()
            pygame.display.flip()
            self.clock.tick(60)
        
        return None
